﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SiteParser.Core.Utilites;
using SiteParser.Core.Logging;

namespace SiteParser.Core.ParserEntities
{
	public class ReferenceParser : ParserEntity
	{
		public bool ReadCacheEnabled { get; set; }
		public bool WriteCacheEnabled { get; set; }
		public string EncodingName { get; set; }

		protected override void ProcessCurrentContext(ParsingContext context, IHtmlIterator htmlIterator) {
			Logger.Info("Processing current context for 'ReferenceParser'");

			if (htmlIterator.MoveNext() && !string.IsNullOrEmpty(htmlIterator.SelectedValue)) {
				string referenceUrl = HttpUrlResolver.CombineUrls(context.CurrentUrl, htmlIterator.SelectedValue);
				string referenceHtmlContent = HttpUrlResolver.GetReferenceStringContent(referenceUrl, EncodingName, ReadCacheEnabled, WriteCacheEnabled);

				ParsingContext referenceContext = new ParsingContext() {
					InnerEntity = context.InnerEntity,
					InnerHtml = referenceHtmlContent,
					CurrentUrl = referenceUrl,
					ParentContext = context
				};

				ProcessChildEntities(referenceContext);
			}
		}
	}
}
